// By buya

const int MEMBERS = 3;

int getAmountInMap(Players@ players, Map@ map){
    int count = 0;
    for(int i=0; i<players.getAmount(); i++)
        if(players[i].getMap().getID() == map.getID() && players[i].getChannel().getID() == map.getChannel().getID())
            count++;
    return count;
}

void npc(Player @player, NPC @npc) {
	int state = npc.getState();
	int map = npc.getPlayer().getMap().getID();
    	if(map == 910010100){
    		if(state == 0){
    			npc.addText("Would you like to go to #rPig Town#k? It is a town where Pigs are everywhere, you might find some valuable items there!");
    			npc.sendYesNo();
    		}
    		else if(state == 1){
    			if(npc.getSelected() == YES){
        			if(@player.getParty() == null || player.getParty().getLeader() != player.getID()){
            				npc.addText("You are not in a party or you are not the party leader.");
            				npc.sendNext();
            				npc.end();
        			}
        			else{
            				Players@ members = player.getParty().getMembers();

            				if(getAmountInMap(members, player.getMap()) != MEMBERS || members.getAmount() != MEMBERS){
                				npc.addText("Your party is not a party of three. Make sure all your members are present and qualified to participate in this quest.");
                				npc.sendNext();
            				}
            				else if(!player.getChannel().createEvent("pigtown", player.getParty())){
                				npc.addText("Some other party is already in pig town. Please try again later.");
                				npc.sendNext();
            				}
					npc.end();
				}
            		}
			if(npc.getSelected() == NO){
				npc.addText("OK be back if you change your mind!");
				npc.sendNext();
				npc.end();
			}
        	}
    	}
    	else if(map == 910010300){
    		npc.getPlayer().changeMap(100000200);
    		npc.end();
  	}
    	else if(map == 910010200){
		if(state == 0){
			npc.addText("Do you really wanna go out?");
			npc.sendYesNo();
		}
		else if(state == 1){
			if(npc.getSelected() == YES){
  				npc.end();
				npc.getPlayer().changeMap(100000200);
			}
			else {
				npc.addText("Ok! Lets go fight the piggy!!");
				npc.sendNext();
				npc.end();
			}
		}
  	}	
}